package com.csii.sys.config;

import com.csii.core.system.vo.LoginUser;
import com.csii.user.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @Description TODO
 * @Date 2020/6/21 7:31
 * @Created by Ian
 * @Contact: 19107614@qq.com
 */
@Component
@Aspect
@Slf4j
public class UserInfoAspect {


    @Pointcut("execution(* com.csii.*.controller.*Controller.*(..))")
    public void userInfoPoint(){

    }
    @Before("userInfoPoint()")
    public void before( JoinPoint joinPoint)  {
        if(SecurityContextHolder.getContext().getAuthentication()==null||SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser"))
        {
            return;
        }
        LoginUser loginUser=new LoginUser();
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof SysUser) {
            BeanUtils.copyProperties(SecurityContextHolder.getContext().getAuthentication().getPrincipal(), loginUser);
        }else
        {

        }
        AppUserContext.setUser(loginUser);
    }
    @Before("userInfoPoint()")
    public void after( JoinPoint joinPoint)  {

        AppUserContext.clean();
    }
}
